package com.sunsharing.common;

import com.sunsharing.common.serialize.Serialization;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: criss
 * Date: 13-4-14
 * Time: 下午9:56
 * To change this template use File | Settings | File Templates.
 */
public class Msg implements Serializable {

    public String reqId;
    public String serviceId;
    public String msg;
    public Map requests = new HashMap();

    private  transient Logger logger = Logger.getLogger(Msg.class);

    protected Logger getLogger()
    {
          if(logger==null)
          {
              logger = Logger.getLogger(Msg.class);
          }
        return logger;
    }
    public void initRequest()
    {
        try
        {
            Document doc= DocumentHelper.parseText(msg);
            Element ele = doc.getRootElement();
            String sendId = ele.element("SenderId").getText();
            String serviceId = ele.element("ServiceId").getText();
            this.reqId = sendId;
            this.serviceId = serviceId;
            Element inputs = ele.element("Inputs");
            if(inputs!=null)
            {
                for(Element input : (List<Element>)inputs.elements())
                {
                    requests.put(input.attributeValue("name"),input.getText());
                }
            }
        }catch (Exception e)
        {
            logger.error("解析入参报文出错",e);
            throw new RuntimeException("无法解析请求报文");
        }
    }

}
